package cn.wolfcode.wolf2w.strategy.util;

import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class StrategyUtils {
    @Autowired
    private RedisService redisService;
    @Autowired
    private IStrategyService strategyServicel;

    public Map<String, Object> numIncr(Long sid, String field, Long num){
        String key = RedisKeys.STRATEGY_HASH.join(sid.toString());

        if (!redisService.hasKey(key)) {
            Strategy strategy = strategyServicel.getById(sid);
            HashMap<String, Object> map = new HashMap<>();
            map.put("viewnum",strategy.getViewnum());
            map.put("replynum",strategy.getReplynum());
            map.put("favornum",strategy.getFavornum());
            map.put("sharenum",strategy.getSharenum());
            map.put("thumbsupnum",strategy.getThumbsupnum());
            map.put("strategyId",strategy.getId());

            redisService.setCacheMap(key,map);
        }

        redisService.incrementCacheMapValue(key,field,num);
        return redisService.getCacheMap(key);
    }
}
